package com.netflexitysolutions.amazonws.ec2.internal.operations;

import com.amazonaws.ec2.doc._2009_04_04.DescribeImageAttributeResponseType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeImageAttributeType;
import com.amazonaws.ec2.doc._2009_04_04.EmptyElementType;
import com.netflexitysolutions.amazonws.ec2.EC2;
import com.netflexitysolutions.amazonws.ec2.ImageAttributeType;
import com.netflexitysolutions.amazonws.ec2.internal.ModelUtil;

public class DescribeImageAttributeOperation extends SingleObjectOperationExecutor<DescribeImageAttributeType, Object> {

	private ImageAttributeType attributeType;
	
	public DescribeImageAttributeOperation(EC2 ec2, String imageId, ImageAttributeType attributeType) {
		super(ec2, imageId);
		this.attributeType = attributeType;
		if (attributeType == null) {
			throw new IllegalArgumentException("ImageAttributeType must be specified");
		}
		if (objectId == null) {
			throw new IllegalArgumentException("ImageId must be specified");
		}
	}

	@Override
	protected Object call(DescribeImageAttributeType request) {
		request.setImageId(objectId);
		switch (attributeType) {
		case BLOCK_DEVICE_MAPPING:
			request.setBlockDeviceMapping(new EmptyElementType());
			break;

		case KERNEL:
			request.setKernel(new EmptyElementType());
			break;

		case LAUNCH_PERMISSION:
			request.setLaunchPermission(new EmptyElementType());
			break;

		case PRODUCT_CODES:
			request.setProductCodes(new EmptyElementType());
			break;

		case RAMDISK:
			request.setRamdisk(new EmptyElementType());
			break;
		}
		
		DescribeImageAttributeResponseType response = getEC2().getService().describeImageAttribute(request);
		
		switch (attributeType) {
		case BLOCK_DEVICE_MAPPING:
			return ModelUtil.toDomainType(response.getBlockDeviceMapping());

		case KERNEL:
			return ModelUtil.toDomainType(response.getKernel());

		case LAUNCH_PERMISSION:
			return ModelUtil.toDomainType(response.getLaunchPermission());

		case PRODUCT_CODES:
			return ModelUtil.toDomainType(response.getProductCodes());

		case RAMDISK:
			return ModelUtil.toDomainType(response.getRamdisk());
		}
		return null;
	}
		
}
